import { resolve } from "path";

export function Tracker(name) {
  this.name = name;
}

Tracker.prototype.setCommonParams = function (defaultConfig) {
  this.config = defaultConfig;
};

Tracker.prototype.sendLog = function (config) {
  const allConfigs = { ...this.config, ...config };
  return fetchApi(this.url, allConfigs);
};

export const tracker = new Tracker("news_system");

// 假如我有两个平台，一个是错误监控平台， 一个是数据分析平台

export function ErrorTracker() {
  this.url = "/xx/error/log";
}

export function AnalysisTracker() {
  this.url = "/xx/analysis/log";
}
export const et = new ErrorTracker("news_system");
export const at = new AnalysisTracker("news_system");

tracker.setCommonParams({
  url,
  route,
  useId,
});

tracker.sendLog({
  action_type,
  event_name,
});

// const p = Object.create(Q);
// p.__proto__ = Q
// const p = new Q();
// p.__proto__ = Q.prototype;

// 一般来说，__proto__ 指向的，一定是一个对象。
// new 的是构造函数嘛~，对象就是 F.prototype

// 手写bind / apply / call
Function.prototype.newBind = function () {
  // 不能使用箭头函数，因为箭头函数不存在默认参数
  let _that = this; // 确保内部能使用到this
  let _agms = Array.prototype.slice.call(arguments);
  let newArray = _agms.shift();
  return function () {
    return new newApply(newArray, _agms);
  };
};
Function.prototype.newApply = function (context) {
  // 边缘检测
  if (typeof this !== "function") {
    throw new Error("this is not defind");
  }
  // 参数兜底
  context = context || window;
  context.fn = this;
  let result = arguments[1] ? context.fn(...arguments[1]) : context.fn();
  delete context.fn;
  return result;
};
// 组合寄生式继承
function fn1() {
  this.name = "hahaha";
  this.array = ["pb"];
}
fn1.prototype.getName = function () {
  return this.name;
};
function fn2() {
  this.age = 11;
}
fn2.prototype.getAge = function () {
  return this.age;
};
function fn3() {
  this.work = "cscs";
  fn1.getName(this, arguments);
  fn2.getAge(this, arguments);
}

fn3.prototype = Object.create(fn1.prototype);
Object.assign(fn1.prototype, fn2.prototype);
fn3.prototype.constructor = fn3;
let old = new fn3();
let old2 = new fn3();
old2.array.push("cs");
// promise A+
const PENDING = "PENDING";
const FULFILLED = "FULFILLED";
const REJECTED = "REJECTED";
class newPromise {
  // 构造器
  constructor(executor) {
    // 流转状态
    this.status = PENDING;
    // 成功/失败值
    this.value = undefined;
    this.reason = undefined;
    // 成功的回调
    this.onResolveCallBacks = [];
    // 失败的回调
    this.onRejectedCallBacks = [];
    // 成功的方法
    const resolve = (resolve) => {
      this.status = FULFILLED;
      this.value = resolve;
      this.onResolveCallBacks.forEach((fn) => fn());
    };
    // 失败的方法
    const rejected = (reject) => {
      this.status = REJECTED;
      this.reason = reject;
      this.onRejectedCallBacks.forEach((fn) => fn());
    };
    // 主执行
    try {
      executor(resolve, rejected);
    } catch (err) {
      rejected(err);
    }
  }
  then(onFulfilled, onRejected) {
    // 成功的状态
    if (this.status === FULFILLED) {
      onFulfilled(this.value);
    }
    // 失败的状态
    if (this.status === REJECTED) {
      onRejected(this.reason);
    }
    // 默认状态
    if (this.status === PENDING) {
      // 存放栈队列
      this.onResolveCallBacks.push(() => onFulfilled(this.value));
      this.onRejectedCallBacks.push(() => onRejected(this.reason));
    }
  }
}
